package com.quanshu.shop.main.view;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quanshu.exchange.R;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.GoodsInfoActivity;
import com.quanshu.shop.main.adapter.ShopMainGoodsFragmentAdapter;
import com.quanshu.shop.main.entity.ShopMainEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopMainGoodsFragment extends Fragment {
    @BindView(R.id.shop_fragment_rlv)
    RecyclerView recyclerView;
    private List<ShopMainEntity.GoodsEntity> goodsEntityList;
    private Context mContext;
    private Unbinder unbinder;
    private ShopMainGoodsFragmentAdapter shopMainGoodsFragmentAdapter;

    public ShopMainGoodsFragment() {
        // Required empty public constructor
    }

    public static ShopMainGoodsFragment newInstance(Context context, List<ShopMainEntity.GoodsEntity> goodsEntityListInit) {
        ShopMainGoodsFragment fragment = new ShopMainGoodsFragment();
        fragment.mContext = context;
        fragment.goodsEntityList = goodsEntityListInit;
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop_main_goods, container, false);
        unbinder = ButterKnife.bind(this, view);
        if (goodsEntityList!=null&&goodsEntityList.size()>0){
            Log.d("测试--", "onSuccess: " + goodsEntityList.size());
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
            gridLayoutManager.setSpanCount(3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setNestedScrollingEnabled(false);
            shopMainGoodsFragmentAdapter = new ShopMainGoodsFragmentAdapter(goodsEntityList, mContext);
            shopMainGoodsFragmentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()) {
                        case R.id.item_recommend_rl:
//                            if (MyApplication.getInstance().getCurrentUser()==null){
//                                BaseTool.goActivity(mContext, LoginActivity.class,null);
//                                return;
//                            }
                            Bundle bundle = new Bundle();
                            bundle.putString("goods_info_id",goodsEntityList.get(position).getId());
                            BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
                            break;
                    }
                }
            });
            recyclerView.setAdapter(shopMainGoodsFragmentAdapter);
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
